package com.xbdun.web.validatebmp.frame.gateway;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.xbdun.common.api.XbdUnObjectResult;
import com.xbdun.common.enums.XbdunPkgEnum;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.util.SpringUtil;
import com.xbdun.web.validatebmp.gataway.RegisterMethodEnum;

import lombok.extern.slf4j.Slf4j;

/**
 * 支付请求
 * @author <a href="zhanggt@cstonline.com">张国涛</a>
 * @createTime 2016年3月7日 下午2:20:18
 */
@Slf4j
public abstract class AbWayController {
	public AbWayController(){}
	@Autowired
	private HttpServletRequest request;
	/**
	 * 设置请求的方法需要的参数
	 * @author ZHANGGUOTAO
	 * @return
	 */
    public Object getServiceMethod(){ 
	//根据参数方法获取到需要返回的类型
    String parameter = request.getParameter("method");
    if(StringUtils.isBlank(parameter))return null;
    RegisterMethodEnum registerMethodEnum = null;
     try{
    	 //获取注入的类路径
    	 registerMethodEnum = BaseWayPathConfig.getRegisterClass(parameter);
    	 //反射给出一个对象
		  return (null==registerMethodEnum||null==registerMethodEnum.getRqtClasses()?null:registerMethodEnum.getRqtClasses().newInstance());
	  }catch(Exception ex){
		  log.error("WayController.getMethod 反射失败类Bean名["+parameter+"],具体类["+(null!=registerMethodEnum&&null!=registerMethodEnum.getRqtClasses()?registerMethodEnum.getRqtClasses().getName():parameter)+"]", ex);
	  }
	  return  null;
    } 
	
	/**
	 * 对外开放统一接口，根据methd方法判断执行BEAN逻辑
	 * @author ZHANGGUOTAO
	 * @param response
	 * @param baseWayRequest
	 * @param commReqeust
	 * @return
	 */
    @SuppressWarnings("rawtypes")
	public XbdUnObjectResult gateway(HttpServletResponse response,BaseMacRequest macRequest,Object commReqeust){
    	//入参检车
    	XbdUnObjectResult result = inputChked(macRequest,commReqeust);
    	if(null!=result)return result;
    	
    	RegisterMethodEnum registerClass = BaseWayPathConfig.getRegisterClass(macRequest.getMethod());
    	log.info("开始-["+registerClass.getMenuName()+"] = ["+macRequest.getMethod()+"],入参:"+(null==commReqeust?"null":JSONObject.toJSONString(commReqeust)));
		//获取到对应bean
		IBaseWayService iBean = (IBaseWayService)SpringUtil.getBean(macRequest.getMethod());
		try{
			if(null!=iBean){
				result = iBean.execute(request, response,macRequest,commReqeust);
			}
			//默认是系统是错误的
			if(null==result&&!RegisterMethodEnum.GENRO_PERSION_QR.getMenuName().equals(registerClass.getMenuCode())){
				result = XbdUnObjectResult.newInstance();
				result.setBizException(BizException.PARAM_CHKE_FAIL.newInstance("请检查[method]参数是否正确,未能找到处理逻辑块"));
			}
		}catch(BizException e){
			result = XbdUnObjectResult.newInstance();
			result.setBizException(e);
			log.warn("警告访问方法["+macRequest.getMethod()+"]",e);
		}catch(Exception ex){
			result = XbdUnObjectResult.newInstance();
			result.setBizException(BizException.SYS_BUSY_0);
			log.error("WayController.gateway 异常["+(null!=macRequest?macRequest.getMethod():"")+"]", ex);
		}finally{
			log.info("结束-["+registerClass.getMenuName()+"] = ["+macRequest.getMethod()+"],结果:"+JSONObject.toJSONString(result));
			registerClass = null;
		}
		return result;
	}
	/**
	 * 基础参数判断是否合法
	 * @author ZHANGGUOTAO
	 * @param baseWayRequest
	 * @return
	 */
	private XbdUnObjectResult inputChked(BaseMacRequest baseWayRequest,Object commReqeust){
		//判断基本参数的合法性
		if(null==commReqeust){
			XbdUnObjectResult result = XbdUnObjectResult.newInstance();
			result.setBizException(BizException.PARAM_CHKE_FAIL.newInstance("请检查[method]参数是否正确,转对象为Null"));
			return result;
		}
		if(null==baseWayRequest){
			XbdUnObjectResult result = XbdUnObjectResult.newInstance();
			result.setBizException(BizException.PARAM_CHKE_FAIL.newInstance("基础签名参数对象不能为空"));
			return result;
		}
		//授权平台
		//if(StringUtils.isBlank(baseWayRequest.getSecretKey())||!NumberUtils.isDigits(baseWayRequest.getSecretKey())){
		if(null==XbdunPkgEnum.getEnum(baseWayRequest.getPkgName())){
			XbdUnObjectResult result = XbdUnObjectResult.newInstance();
			result.setBizException(BizException.PARAM_CHKE_FAIL.newInstance("授权平台不合法"));
			return result;
		}
		if(0>=baseWayRequest.getTimestamp()){
			XbdUnObjectResult result = XbdUnObjectResult.newInstance();
			result.setBizException(BizException.PARAM_CHKE_FAIL.newInstance("时间戳必须大于零"));
			return result;
		}
		//签名信息
		if(StringUtils.isBlank(baseWayRequest.getMac())){
			XbdUnObjectResult result = XbdUnObjectResult.newInstance();
			result.setBizException(BizException.PARAM_CHKE_FAIL.newInstance("签名信息不能为空mac"));
			return result;
		}
		return null;
	}
}
